/**
 * Created by yangfan on 2016/10/19.
 */
/**
 * Created by yangfan on 2016/8/15.
 */
define(["business/services/constant"], function (constant) {

    //var baseUrl = "http://192.168.1.14:8082";
        var baseUrl = constant.config.BASEURL_LIBRARY;
    var data_url = constant.config.BASEURL_LIBRARY;
    var databaseURL = constant.config.BASEURL_DATABASE;
    var tag_api = constant.config.CONSTANT_LIBTAG;

    var preview_URL = constant.config.BASEURL_PDF;
    var preview_api = constant.config.CONSTANT_PDF;
    var uploadURL = constant.config.BASEURL_LIBRARY;
    var uploadAPI = constant.config.CONSTANT_LIBTHEISE;
    var CONSTANT_DATAFILE = "elink_database/api/file";
    var CONSTANT_LIBRARY = "elink_library/api/category";
    var CONTSTANT_FILETYPE = "elink_database/api/type/list";
    var CONSTANT_THESIS = "elink_library/api/thesis";
    var CONSTANT_COLLECTION = "elink_library/api/collection";
    var CONSTANT_COMMENT = "elink_library/api/comment";

    var injector = angular.injector(["ng"]);
    var token = "seed="+ localStorage.seed + "&authToken=" + localStorage.authToken;
    var filterRequestion = function(requestion){
        if(requestion.data){
            var str="";
            for(var key in requestion.data){
                if(requestion.data[key]==="undefined"||requestion.data[key]===""||requestion.data[key]==NaN||requestion.data[key]==null){

                }else{
                    str += key +"="+requestion.data[key]+"&"
                }
            }
            str = str.substr(0,str.lastIndexOf("&"));
            return str + "&" + token;
        }
    };
    return {
        getTopCategory :function(requestion){
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_LIBRARY+"/"+requestion.uri + "?" + token
                })
            };
            return  injector.invoke(httpSend)
        },
        getSecondaryCategory:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_LIBRARY+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getDetailCategory:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_LIBRARY+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },

        getTypeList :function(requestion){
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:databaseURL+"/"+CONTSTANT_FILETYPE + "?" + token
                })
            };
            return  injector.invoke(httpSend)
        },
        getThesisList:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_THESIS+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getDownLoadRank:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_THESIS+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getRelevantType:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_THESIS+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getRelevant:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_THESIS+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getKeyWord:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_THESIS+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getThesis:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_THESIS+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getFileFromData:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:data_url+"/"+CONSTANT_DATAFILE+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getLike:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_THESIS+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        updateCollection:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_COLLECTION+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getTopCommentsById:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseUrl+"/"+CONSTANT_COMMENT+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        addFile:function(requesion){
            return constant.httpSend(uploadURL,uploadAPI,requesion);
        },
        tagServices:function(requestion){
            return constant.httpSend(baseUrl,tag_api,requestion);
        },
		libraryDownloadHttpService:function(requesion){
   			return constant.httpSend(baseUrl,CONSTANT_THESIS,requesion);
   		},
        getPreview:function(requestion){
            return constant.httpSend(preview_URL,preview_api,requestion);
        }





    }
});

